﻿using System;
using System.Collections.Generic;
using System.Text;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using Transcend.DP3.Helper;

namespace Transcend.DP3.Db
{
	public class ODPHelper
	{

		private string dateTimeExactFormat = "yyyy-MM-dd HH:mm:ss";
		/// <summary>
		/// 包括时间的日期解析格式
		/// </summary>
		public string DateTimeExactFormat
		{
			get { return dateTimeExactFormat; }
			set { dateTimeExactFormat = value; }
		}

		private string dateExactFormat = "YYYY-MM-dd";
		/// <summary>
		/// 日期解析格式
		/// </summary>
		public string DateExactFormat
		{
			get { return dateExactFormat; }
			set { dateExactFormat = value; }
		}

		/// <summary>
		/// 将值转换成odp类型指定类型
		/// </summary>
		/// <param name="oraType"></param>
		/// <param name="val"></param>
		/// <returns></returns>
		public object GetODPValue(int oraType, object val)
		{
			object ret = null;
			if (val == null) return DBNull.Value;
			if (val is DBNull) return val;
			OracleDbType oraDbType = (OracleDbType)oraType;
			switch (oraDbType)
			{
				// date
				case OracleDbType.Date:
				case OracleDbType.TimeStamp:
				case OracleDbType.TimeStampLTZ:
				case OracleDbType.TimeStampTZ:
					if (val is OracleDate)
					{
						ret = (OracleDate)val;
					}
					else
					{
						ret = new OracleDate(val is DateTime ? (DateTime) val : DateTime.Parse(val.ToString()));
					}
					break;
				// byte
				case OracleDbType.Byte:
					ret = ToOdpDec(val);
					break;
				// double
				case OracleDbType.Double:
					ret = ToOdpDec(val);
					break;
				// short
				case OracleDbType.Int16:
					ret = ToOdpDec(val);
					break;
				// int
				case OracleDbType.Int32:
					ret = ToOdpDec(val);
					break;
				// long
				case OracleDbType.Int64:
					ret = ToOdpDec(val);
					break;
				case OracleDbType.IntervalYM:
					if (val is OracleIntervalYM)
					{
						ret = (OracleIntervalYM)val;
					}
					else
					{
						ret = new OracleIntervalYM(ConvertHelper.DblVal(val.ToString(), 0));
					}
					break;
				// decimal
				case OracleDbType.Decimal:
				case OracleDbType.Single:
				case OracleDbType.BinaryDouble:
				case OracleDbType.BinaryFloat:
					ret = ToOdpDec(val);
					break;
				// string
				case OracleDbType.Char:
				case OracleDbType.NChar:
				case OracleDbType.NVarchar2:
				case OracleDbType.Varchar2:
				case OracleDbType.Long:
				case OracleDbType.LongRaw:
					if (val is OracleString)
					{
						ret = (OracleString)val;
					}
					else
					{
						ret = new OracleString(val is string ? val as string : val.ToString());
					}
					break;
				case OracleDbType.Clob:
				case OracleDbType.NClob:
					if (val is OracleString)
					{
						ret = (OracleString)val;
					}
					else
					{
						ret = val is string ? new OracleString(val as string) : new OracleString(val.ToString());
					}
					break;
				// TimeStan
				case OracleDbType.IntervalDS:
					ret = new OracleIntervalDS(val is TimeSpan ? (TimeSpan)val : new TimeSpan(Convert.ToInt64(val)));
					break;
				// binary
				case OracleDbType.Raw:
				case OracleDbType.BFile:
				case OracleDbType.Blob:
				case OracleDbType.RefCursor:
				case OracleDbType.XmlType:
				case OracleDbType.Array:
				case OracleDbType.Object:
				case OracleDbType.Ref:
				default:
					ExHelper.InvalidOperation(string.Format("数据类型{0}不受支持", Enum.GetName(typeof(OracleDbType), oraDbType)));
					break;
			}
			return val;
		}

		/// <summary>
		/// 将对象类型转换成OracleDecimal
		/// </summary>
		/// <param name="val"></param>
		/// <returns></returns>
		private OracleDecimal ToOdpDec(object val)
		{
			if (val == null) return OracleDecimal.Zero;
			if (val is OracleDecimal)
			{
				return (OracleDecimal) val;
			}

			if (ConvertHelper.IsNumber(val))
			{
				return new OracleDecimal((decimal)val);
			}

			return new OracleDecimal(val.ToString());
		}

	}

}
